from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel, QComboBox,
                             QCheckBox, QLineEdit, QDialogButtonBox, QGroupBox)


class ExportDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("导出试题")
        self.setup_ui()

    def setup_ui(self):
        layout = QVBoxLayout()
        self.setFixedSize(600, 400)
        # 筛选条件组
        filter_group = QGroupBox("筛选条件")
        filter_layout = QVBoxLayout()

        # 题目类型
        type_layout = QHBoxLayout()
        type_layout.addWidget(QLabel("题目类型:"))
        self.type_combo = QComboBox()
        self.type_combo.addItem("所有类型", "")
        self.type_combo.addItem("填空题", "填空题")
        self.type_combo.addItem("选择题", "选择题")
        self.type_combo.addItem("判断题", "判断题")
        self.type_combo.addItem("综合题", "综合题")
        type_layout.addWidget(self.type_combo)
        filter_layout.addLayout(type_layout)

        # 难度
        difficulty_layout = QHBoxLayout()
        difficulty_layout.addWidget(QLabel("难度:"))
        self.difficulty_combo = QComboBox()
        self.difficulty_combo.addItem("所有难度", 0)
        for i in range(1, 6):
            self.difficulty_combo.addItem(f"{i}星难度", i)
        difficulty_layout.addWidget(self.difficulty_combo)
        filter_layout.addLayout(difficulty_layout)

        # 分类
        category_layout = QHBoxLayout()
        category_layout.addWidget(QLabel("分类:"))
        self.category_combo = QComboBox()
        self.category_combo.addItem("所有分类", "")
        category_layout.addWidget(self.category_combo)
        filter_layout.addLayout(category_layout)

        # 标签
        tags_layout = QHBoxLayout()
        tags_layout.addWidget(QLabel("标签:"))
        self.tags_edit = QLineEdit()
        self.tags_edit.setPlaceholderText("多个标签用逗号分隔")
        tags_layout.addWidget(self.tags_edit)
        filter_layout.addLayout(tags_layout)

        # 收藏
        self.favorite_check = QCheckBox("仅导出收藏题目")
        filter_layout.addWidget(self.favorite_check)

        filter_group.setLayout(filter_layout)
        layout.addWidget(filter_group)

        # 导出选项组
        options_group = QGroupBox("导出选项")
        options_layout = QVBoxLayout()

        # 导出格式
        format_layout = QHBoxLayout()
        format_layout.addWidget(QLabel("导出格式:"))
        self.format_combo = QComboBox()
        self.format_combo.addItem("Word文档 (.docx)", "docx")
        # self.format_combo.addItem("PDF文档 (.pdf)", "pdf")
        self.format_combo.addItem("Markdown (.md)", "md")
        format_layout.addWidget(self.format_combo)
        options_layout.addLayout(format_layout)

        # 包含图片
        self.include_images_check = QCheckBox("包含题目和答案图片")
        self.include_images_check.setChecked(True)
        # options_layout.addWidget(self.include_images_check)

        # 包含答案
        self.include_answers_check = QCheckBox("包含题目答案")
        self.include_answers_check.setChecked(True)
        # options_layout.addWidget(self.include_answers_check)

        options_group.setLayout(options_layout)
        layout.addWidget(options_group)

        # 按钮
        button_box = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        button_box.accepted.connect(self.accept)
        button_box.rejected.connect(self.reject)
        layout.addWidget(button_box)

        self.setLayout(layout)

    def get_export_settings(self):
        """获取导出设置"""
        return {
            'filter': {
                'question_type': self.type_combo.currentData(),
                'difficulty': self.difficulty_combo.currentData(),
                'category': self.category_combo.currentData(),
                'tags': self.tags_edit.text().strip(),
                'only_favorite': self.favorite_check.isChecked()
            },
            'options': {
                'format': self.format_combo.currentData(),
                'include_images': self.include_images_check.isChecked(),
                'include_answers': self.include_answers_check.isChecked()
            }
        }

    def load_categories(self, categories):
        """加载分类数据"""
        self.category_combo.clear()
        self.category_combo.addItem("所有分类", "")
        for category in categories:
            self.category_combo.addItem(category, category)
